<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="hAdd(1,0)">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button v-if="scope.row.type===1" type="text" @click="hAdd(2,scope.row.id)">添加</el-button>
              <el-button v-focus="'baoc'" type="text" @click="hEdit(scope.row.id)">编辑</el-button>
              <el-button type="text" @click="hDel(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <el-dialog
        :visible.sync="showDialog"
        :title="isEdit?'编辑':'添加'"
        @close="resetForm"
      >
        <!-- 表单内容 -->
        <el-form ref="form" label-width="100px" :model="formData" :rules="rules">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-text="启用"
              active-value="1"
              inactive-text="不启用"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="showDialog = false">取消</el-button>
            <el-button type="primary" @click="hSubmit">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>
<script>
// 获取权限列表,添加,详情,权限编辑
import { getPermissionList, addPermission, getPermissionDetail, updatePermission, delPermission } from '@/api/permission'
// 平铺数据转树形结构
import { tranListToTreeData } from '@/utils'

export default {

  data() {
    // 部门名字验证规则
    const nameRules = (rul, value, callback) => {
      // if (this.formData.type === 1) {
      //   console.log('qqqqqq')
      //   const nameList = this.list.map(item => item.name)
      //   console.log(nameList)
      //   console.log(nameList.includes(value))
      //   if (nameList.includes(value)) {
      //     callback(new Error('该名字以在部门中存在'))
      //   } else {
      //     callback()
      //   }
      // }
      // 找到当前pid相同的元素
      let nameList = this.originList.filter(item => item.pid === this.formData.pid)
      if (this.isEdit) {
        nameList = this.originList.filter(item => item.pid === this.formData.pid && item.id !== this.formData.id)
      }
      nameList.map(it => it.name).includes(value) ? callback(new Error(value + '已经占用')) : callback()
    }

    // 标识验证规则
    const codeRules = (rule, value, callback) => {
      let codeList = this.list.map(item => item.code)
      // console.log(rule)
      // console.log(value)
      if (this.isEdit) {
        codeList = this.list.filter(item => item.id !== this.formData.id).map(item => item.code)
      }
      // console.log(codeList)
      // console.log(codeList.includes(value))

      codeList.includes(value) ? callback(new Error('当前标识以存在' + value + '部门')) : callback()
    }
    return {
      list: [], // 保存权限列表数据
      showDialog: false, // 是否显示弹层
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      },
      originList: [],
      // 表单验证
      rules: {
        name: [{ required: true, message: '权限名称不能为空', triggler: 'bulr' },
          { validator: nameRules, triggler: 'bulr' }
        ],
        code: [{ required: true, message: '权限名称不能为空', triggler: 'bulr' },
          { validator: codeRules, triggler: 'bulr' }
        ],
        description: [{ required: true, message: '权限名称不能为空', triggler: 'bulr' }]
      },
      // 添加还是编辑状态
      isEdit: false

    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 获取权限数据列表
    async getPermissionList() {
      const res = await getPermissionList()
      console.log(res)
      this.originList = res.data
      this.list = tranListToTreeData(res.data)

      // console.log(res)
    },
    // 添加权限方法
    async doAdd() {
      try {
        await addPermission(this.formData)
        // console.log(res)
        // 1.关闭弹窗
        this.showDialog = false
        // 2.重新请求数据
        this.getPermissionList()
        // 3.提示信息
        this.$message.success('添加成功')
      } catch (err) {
        this.$message.error('添加失败')
        console.log(err)
      }
    },
    // 编辑权限API
    async doEdit() {
      try {
        await updatePermission(this.formData)
        // console.log(res)
        // 1.关闭弹窗
        this.showDialog = false
        // 2.重新请求数据
        this.getPermissionList()
        // 3.提示信息
        this.$message.success('编辑成功')
      } catch (err) {
        this.$message.error('编辑失败')
        console.log(err)
      }
    },
    // 删除API
    async doDel(id) {
      try {
        const res = await delPermission(id)
        console.log(res)

        this.getPermissionList()
        this.$message.success('删除成功')
      } catch (err) {
        console.log(err)
        this.$message.error('删除失败')
      }
    },
    // 添加权限按钮
    hAdd(type, id) {
      // 1.打开弹窗
      this.showDialog = true

      // 2.保存数据
      console.log(id)
      console.log(type)
      this.formData.type = type
      this.formData.pid = id
      // console.log(this.formData.type)
      // 3.确定添加状态
      this.isEdit = false
    },
    // 编辑权限按钮
    async hEdit(id) {
      // alert(id)
      // 1.打开弹窗
      this.showDialog = true
      // 2.确定编辑状态
      this.isEdit = true
      // 3.请求数据
      const res = await getPermissionDetail(id)
      // 4.数据回填
      this.formData = res.data
    },
    // 删除按钮
    hDel(id) {
      this.$confirm('确定要删除吗？').then(() => {
        this.doDel(id)
      }).catch(() => {})
      // console.log(id)
    },
    // 确认提交
    hSubmit() {
      // console.log(1)
      this.$refs.form.validate(valid => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    // 弹窗关闭
    resetForm() {
      // 1.清空表单数据
      this.formData = {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      }
      // 2.清空表单验证
      this.$refs.form.resetFields()
    }
  }

}
</script>

